package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.gmc.global.g.rest.service.ITenantTrademarkAuthInfoService;
import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.gmc.global.service.common.constants.enums.TenantAuditStatusEnum;
import com.ibeeking.gmc.global.service.common.dos.TenantTrademarkAuthInfoDO;
import com.ibeeking.gmc.global.service.common.param.TenantTrademarkAuthInfoParam;
import com.ibeeking.gmc.global.service.common.vo.TenantTrademarkAuthInfoVO;
import com.ibeeking.gmc.global.service.mapper.TenantTrademarkAuthInfoMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @ClassName TenantTrademarkAuthInfoServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-12-01 16:35
 **/
@Service
public class TenantTrademarkAuthInfoServiceImpl extends ServiceImpl<TenantTrademarkAuthInfoMapper, TenantTrademarkAuthInfoDO> implements ITenantTrademarkAuthInfoService {

    @Resource
    private TenantTrademarkAuthInfoMapper tenantTrademarkAuthInfoMapper;

    @Resource
    private FileOssProp fileOssProp;

    @Override
    public TenantTrademarkAuthInfoVO queryByTenantId(Integer tenantId) {
        TenantTrademarkAuthInfoDO tenantTrademarkAuthInfoDO = tenantTrademarkAuthInfoMapper.selectOne(Wrappers.lambdaQuery(TenantTrademarkAuthInfoDO.class).eq(TenantTrademarkAuthInfoDO::getTenantId, tenantId));
        if (tenantTrademarkAuthInfoDO == null) {
            return null;
        }
        tenantTrademarkAuthInfoDO.setRegistrationDoc(UrlUtils.addUrlDomainName(fileOssProp.getHost(), tenantTrademarkAuthInfoDO.getRegistrationDoc()));
        tenantTrademarkAuthInfoDO.setAuthDoc(UrlUtils.addUrlDomainName(fileOssProp.getHost(), tenantTrademarkAuthInfoDO.getAuthDoc()));
        return JsonUtils.fromJson(tenantTrademarkAuthInfoDO, TenantTrademarkAuthInfoVO.class);
    }

    @Override
    public Boolean save(TenantTrademarkAuthInfoParam param) {
        TenantTrademarkAuthInfoVO tenantTrademarkAuthInfoVO = getBean().queryByTenantId(param.getTenantId());
        Long userId = UserUtils.getUserId();
        if (tenantTrademarkAuthInfoVO != null) {
            param.setId(tenantTrademarkAuthInfoVO.getId());
            param.setAuditStatus(tenantTrademarkAuthInfoVO.getAuditStatus());
            param.setCreateBy(userId);

        } else {
            param.setAuditStatus(TenantAuditStatusEnum.SUBMIT_NO.getValue());
            param.setModifyBy(userId);
        }
        param.setRegistrationDoc(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(), param.getRegistrationDoc()));
        param.setAuthDoc(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(), param.getAuthDoc()));
        TenantTrademarkAuthInfoDO tenantTrademarkAuthInfoDO = JsonUtils.fromJson(param, TenantTrademarkAuthInfoDO.class);
        tenantTrademarkAuthInfoDO.setCreateTime(LocalDateTime.now());
        tenantTrademarkAuthInfoDO.setModifyTime(LocalDateTime.now());
        getBean().saveOrUpdate(tenantTrademarkAuthInfoDO);
        return true;
    }

    @Override
    public Boolean changeStatus(Integer tenantId, Integer auditStatus) {
        DataUtils.trueThrowException(tenantId == null || auditStatus == null, "审核状态修改失败!");
        return getBean().update(Wrappers.lambdaUpdate(TenantTrademarkAuthInfoDO.class).eq(TenantTrademarkAuthInfoDO::getTenantId, tenantId).set(TenantTrademarkAuthInfoDO::getAuditStatus, auditStatus));
    }

    private static TenantTrademarkAuthInfoServiceImpl getBean() {
        return SpringBeanUtil.getBean(TenantTrademarkAuthInfoServiceImpl.class);
    }

}